package com.wehealth.biz_ecg.helper;

import static com.wehealth.three.lib_connect.utils.EcgFileUtil.saveEcgDataToXMLFile;

import android.app.Application;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.wehealth.biz_ecg.R;
import com.wehealth.ecg.jni.analyse.EcgAnalyse;
import com.wehealth.three.lib_connect.constant.CommonKeyConstant;
import com.wehealth.three.lib_connect.utils.AppGlobals;
import com.wehealth.three.lib_connect.utils.ECGDataAutoAnalyseUtils;
import com.wehealth.biz_ecg.utils.PDFUtils;
import com.wehealth.three.lib_connect.utils.StringUtils;
import com.wehealth.three.lib_connect.utils.VersionUtilsKt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SaveFileAutoThread {
    //文件保存成功
    private final int SAVE_PDF_XML_FILE_SUCCESS = 897;
    //提示对话框
    private final int SHOW_PROGRESS_DIALOG = 800;
    private EcgAnalyse ecgAnalyse;
    private Handler handler;
    private String xmlFilePath;

    private String pdfFilePath;
    private PDFUtils pdfUtils;
    private ExecutorService executorService;

    public SaveFileAutoThread(Handler handle) {
        executorService = Executors.newFixedThreadPool(5);
        handler = handle;
        ecgAnalyse = new EcgAnalyse();
        ecgAnalyse.Axis = new int[3];
        ecgAnalyse.ecgResult = new int[12];
        pdfUtils = new PDFUtils();
    }

    public void startAutoTask(Map<String, Object> map) {
        executorService.submit(new AutoTask(map));
    }

    public class AutoTask implements Runnable {
        private Map<String, Object> map;

        public AutoTask(Map<String, Object> mapDatas) {
            map = mapDatas;
        }

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            Message msg = handler.obtainMessage(SHOW_PROGRESS_DIALOG);
            Application app = AppGlobals.INSTANCE.get();
            String prompt = app.getResources().getString(R.string.prompt_saving_data);
            msg.obj = prompt;
            handler.sendMessage(msg);
            Map<String, Map<String, String>> listMaps = new HashMap<>();
            Map<String, String> pInfo = new HashMap<>();
            Map<String, String> analyses = new HashMap<>();
            //测量人的信息
            String patId = (String) map.get("PAT_ID");
            String patName = (String) map.get("PAT_NAME");
            int patAge = (Integer) map.get("PAT_AGE");
            int patGender = (Integer) map.get("PAT_GENDER");
            pInfo.put("Name", patName);
            pInfo.put("ID", patId);
            pInfo.put("Gender", String.valueOf(patGender));
            pInfo.put("AGE", String.valueOf(patAge));
            pInfo.put("ecg_checktime", String.valueOf(System.currentTimeMillis()));

            //测量的心电信息
            String FilterBase = (String) map.get("FilterBase");
            String FilterMC = (String) map.get("FilterMC");
            String FilterAC = (String) map.get("FilterAC");
            String FilterLP = (String) map.get("FilterLP");
            int waveGain = (Integer) map.get("waveGain");
            int waveSpeed = (Integer) map.get("waveSpeed");
             int waveDisplayType = (Integer) map.get("waveDisplayType");
            List<int[]> ecgDataBuffer = (List<int[]>) map.get("ecgDataBuffer");
            int[] ecgAnas = StringUtils.getEcgDataINTs(ecgDataBuffer);
            //自动分析结论
            ecgAnalyse.initEcgAnalyseLib(3495.2533333f * 3);
            int[] paceBuffer = new int[5000];
            ecgAnalyse.analyseEcgData(ecgAnalyse, ecgAnas, 5000, 12, paceBuffer, 0, 0);
            int[] axis = ecgAnalyse.Axis;
            int[] resultCode = ecgAnalyse.ecgResult;
            int typEcg = ecgAnalyse.typeEcg;
            float ecg_version = ecgAnalyse.getEcgDllVersion();
            String Auto_Result = ECGDataAutoAnalyseUtils.analyseECGDataResult(typEcg, resultCode);
            int hr = ecgAnalyse.HR;
            int pr = ecgAnalyse.PR;
            int qrs = ecgAnalyse.QRS;
            int qt = ecgAnalyse.QT;
            int qtc = ecgAnalyse.QTc;
            int rr = ecgAnalyse.RR;
            int rv5 = ecgAnalyse.RV5;
            int sv1 = ecgAnalyse.SV1;
            analyses.put("HeartRate", String.valueOf(hr));
            analyses.put("PRInterval", String.valueOf(pr));
            analyses.put("RRInterval", String.valueOf(rr));
            analyses.put("QRSDuration", String.valueOf(qrs));
            analyses.put("QTD", String.valueOf(qt));
            analyses.put("QTC", String.valueOf(qtc));
            analyses.put("RV5", String.valueOf(rv5));
            analyses.put("SV1", String.valueOf(sv1));
            analyses.put("RV5SV1", String.valueOf((Math.abs(rv5) + Math.abs(sv1))));
            analyses.put("PAxis", String.valueOf(axis[0]));
            analyses.put("QRSAxis", String.valueOf(axis[1]));
            analyses.put("TAxis", String.valueOf(axis[2]));
            analyses.put("AnalysesVersion", String.valueOf(ecg_version));
            analyses.put("Auto_Result", Auto_Result);
            analyses.put("Gain", String.valueOf(waveGain));
            analyses.put("Speed", String.valueOf(waveSpeed));
            analyses.put("DisplayType", String.valueOf(waveDisplayType));
            analyses.put("FilterBase", FilterBase);
            analyses.put("FilterMC", FilterMC);
            analyses.put("FilterAC", FilterAC);
            analyses.put("FilterLP", FilterLP);
            listMaps.put(CommonKeyConstant.ECG_PATIENT_INFO, pInfo);
            listMaps.put(CommonKeyConstant.ECG_ANALYSE_PARAM, analyses);
            Map<Integer, int[]> datas = StringUtils.praseIntData(ecgDataBuffer);
            Map<String, String> dataString = StringUtils.praseIntDataToString(ecgDataBuffer);
            xmlFilePath = saveEcgDataToXMLFile(AppGlobals.INSTANCE.get(), listMaps, dataString);
            msg = handler.obtainMessage(SAVE_PDF_XML_FILE_SUCCESS);
            pdfFilePath =  pdfUtils.savePDF(true,AppGlobals.INSTANCE.get(), listMaps, datas, waveGain, waveSpeed, waveDisplayType);
            msg.obj = xmlFilePath+"|"+pdfFilePath;
            handler.sendMessage(msg);
        }
    }
}
